package operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024 —03 —04
 * Time: 20:56
 */
public class DelOperation implements IOPeration {
    public void work(Booklist booklist) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书");
        String name = scanner.nextLine();
        int pos = -1;

        int currentSize = booklist.getUsedsize();
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            if (book.getName().equals(name)){
                pos = i;
                break;
            }
            if (i == currentSize){
                System.out.println("没有你要删除的图书！");
                return;
            }
        }
//开始删除

        int j = pos;//妙啊妙啊妙啊
        for (; j < currentSize-1 ; j++) {
            Book book = booklist.getBook(j+1);
            booklist.setBooks(j,book);
        }
        //到了书架的最后一个位置，j+1没有这个值，不能实现覆盖，所以
        booklist.setBooks(j,null);

        booklist.setUsedsize(currentSize-1);

    }



}
